package com.kongchengji.spider.station.process;
import com.kongchengji.spider.station.constant.Constant;
import com.kongchengji.spider.station.domain.PictureModelDO;
import com.kongchengji.spider.station.process.processImpl.WangYiStationProcess;
import com.kongchengji.spider.station.service.PictureModelService;
import com.kongchengji.spider.util.*;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import us.codecraft.webmagic.selector.Html;

import java.io.IOException;
import java.util.List;

/**
 * 公共方法的抽取
 */
public abstract class AnalysisRequesAbstract implements AnalysisRequestInerface {
    PictureModelService pictureModelService = SpringContextUtil.getBean(PictureModelService.class);
    Logger logger = LoggerFactory.getLogger(WangYiStationProcess.class);
    /**
     * 根据url访问内容页并处理内容页,同时将pictureModelDO对象转json格式访问采集后台接口,这部分是通用逻辑
     *
     * @param urlList
     */
    @Override
    public void contentHandler(List<String> urlList, Constant.Station station, String...rules) throws IOException, InterruptedException {
        Request request;
        OkHttpClient client = SingleOkhttpClientUtil.getInstance();
        for (String url : urlList) {
            request = new Request.Builder()
                    .addHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8")
                    .addHeader("Accept-Language", "zh-CN,zh;q=0.9")
                    .addHeader("Content-Encoding", "gzip")
                    .addHeader("Content-Language", "zh-CN")
                    .addHeader("Host", "pp.163.com")
                    .addHeader("Pragma", "no-cache")
                    .addHeader("Upgrade-Insecure-Requests", "1")
                    .addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36")
                    .url(url).build();
            Response response = client.newCall(request).execute();
            Html html = new Html(response.body().string(), url);
            //释放资源
            response.close();
            //组装对象
            PictureModelDO pictureModelDO = new PictureModelDO();
            //根据html对象获得栏目
            List<String> typeList = html.xpath(rules[0]).all();
            //获取标题
            String title = html.xpath(rules[1]).toString();
            //清除标题两端空格
            title = StringUtils.trim(title);
            //设置标题
            pictureModelDO.setTitle(title);
            //设置图集的唯一标识
            pictureModelDO.setThumbnailUrl(url);
            //获得大图
            List<String> imgsrcList = html.xpath(rules[2]).all();
            //排除为空的元素
            RemoveNullUtil.removeNull(imgsrcList);
            //设置采集网站的质量
            pictureModelDO.setQuality(station.getQuality());
            //设置采集源
            pictureModelDO.setStationId(station.getId());
            //将图集保存进对象
            pictureModelDO.setUrlContentList(imgsrcList);
            //处理导航栏目的同时保存进对象
            ManageTypeListUtil.manageType1(typeList, title, pictureModelDO);
            //释放资源
            response.close();
            //保存数据
            //当时测试时将爬取的数据存储在本地数据库。
//            R r = pictureModelService.add(pictureModelDO,station.getId());
//            logger.info(r.toString());
            //通过接口更新采集后台数据.
            AccessInterfaceUtil.accessInterface(pictureModelDO);
            //控制访问频率
            Thread.sleep(300);
        }
    }
}
